<?php
/**
 * 公共方法
 * Created by PhpStorm.
 * User: Rick
 * Date: 2020/12/9
 * Time: 22:11
 */

function curl_get($url){

    $header = array(
        'Accept: application/json',
    );
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    //执行命令
    $data = curl_exec($curl);
    return $data;
}


/**
 * 影视采集
 * @param $url
 * @return mixed
 */
function collection($url)
{
    $aa =  curl_get($url);
    $dom  = new \DOMDocument();
    @$dom->loadHTML($aa);
    $data['name'] = $dom->getElementsByTagName('h2')->item(0)->nodeValue;
    $data['director'] = $dom->getElementsByTagName('ul')->item(4)->getElementsByTagName('li')->item(1)->getElementsByTagName('span')->item(0)->nodeValue;
    $data['performer'] = $dom->getElementsByTagName('ul')->item(4)->getElementsByTagName('li')->item(2)->getElementsByTagName('span')->item(0)->nodeValue;
    $data['type_name'] = $dom->getElementsByTagName('ul')->item(4)->getElementsByTagName('li')->item(3)->getElementsByTagName('span')->item(0)->nodeValue;
    $data['desc'] = $dom->getElementsByTagName('span')->item(15)->getAttribute('txt');
    $data['img'] = $dom->getElementsByTagName('img')->item(1)->getAttribute('src');
    $data['url'] = $dom->getElementById('down_1')->getElementsByTagName('input')->item(0)->getAttribute('value');
    return $data;
}

/**
 *  json格式返回
 * @param int $code
 * @param $message
 * @param array $data
 * @return \Illuminate\Http\JsonResponse
 */
function ajax($code =200,$message,$data = [])
{

    return response()->json([
        'code'=>$code,
        'message'=>$message,
        'data'=>$data,
        'time'=>time()
    ],$code);
}

/**
 * 验证 token
 * @param $_token
 * @return array
 */
function token_check($_token)
{
    $check_token = \App\Models\User::where('api_token',$_token)->where('expire_time','>',time())->exists();
   return $check_token;
}